
//player globalChat format[" PARAM : %1",_this select 0];

_clickPos=_this select 0;

//_civil=_clickPos nearestObject "Civilian";

 RespawnTrigger setPos _clickPos;
 sleep 0.5;
 _civil=((list RespawnTrigger) select 0);
format[" PARAM : %1",(list RespawnTrigger)]  call debug;
_bestDist=10000000;

{
	_dist=_x distance _clickPos;
	"click on map : "+str _x+" => "+str _dist call debug;
	if(_dist<_bestDist)then{
		_civil=_x;
		_bestDist=_dist;
	};
	
}foreach (list RespawnTrigger);
//_civil=[_clickPos select 0,_clickPos select 1] nearestObject "Civilian";
//_civil=driver vehicle _civil;
if(isDebug)then{

	"debug" setMarkerPos getPos _civil;
	"click on map : "+str _civil call debug;
};


for [{_c=0},{_c<(count CivilsDetectingHumans)},{_c=_c+1}]  do
{
		scopeName "civilLoop";
		
		if(_civil in ((CivilsDetectingHumans select _c) select 1))then
		{
			CurrentCivil=_civil;
			// civil found so respawn in it !
			IsIntoACivil=true;
			
			// remove map click action
			//onMapSingleClick "";
			
            format[" Teleport : %1",_civil] call debug;
            _civilPos=getPos _civil;
            _civilDir=getDir _civil;

            // camera effect
            _camera = "camera" camCreate [0,0,200];

            _camera camSetPos [(_civilPos select 0)+10,(_civilPos select 1)+10,(_civilPos select 2)+10];
            _camera camSetTarget [(_civilPos select 0),(_civilPos select 1),(_civilPos select 2)+1.5];

            _camera camSetFov 0.700;
            _camera cameraEffect ["internal","back"];
            _camera camCommit 0;
			
						
            waitUntil { camCommitted _camera };

            //_camera camSetFov 0.50;
           // _camera camCommit 5;

            //sleep 2;
			
			camFollowUnit=_civil;
			
			
				
			
           // "-1-" call debug;
            //_camHandle=[] spawn CamFollowCivil;
			
			lastTime=time;
			
			while{(time - lastTime)<3}do{
				_civilPosCam=getPos (camFollowUnit);    
				
				// replace the camera if the civil moved during the cutscene
				_camera camSetTarget [(_civilPosCam select 0),(_civilPosCam select 1),(_civilPosCam select 2)+1.5];
				_camera camCommit 1;
				//waitUntil { camCommitted _camera };
				//lastTime=time;
				//str time+" : "+str (time - lastTime) call debug;
				sleep (1/50);
			};

			_civilPos=getPos _civil;
			
			if(_civil isKindOf "LandVehicle")then{
				_vehicle=_civil;
				_civil=driver _civil;
				CurrentCivil=_civil;
				moveOut _civil;
				moveOut player;
				sleep 0.2;
				player moveInDriver _vehicle;
				_civil setDamage 0;
			}else{
				player setPos _civilPos;
				player setDir _civilDir;
			};
			
			//"-2-" call debug;
			
            _civil setPos getMarkerPos "CivilPhantomZone";//[(_civilPos select 0)+100,(_civilPos select 1)+100,0];   
			
			camFollowUnit=player;
			//"-3-" call debug;
			//_camHandle=[] spawn CamFollowCivil;
			
            //waitUntil { camCommitted _camera };
			
			sleep 1;
			//"-4-" call debug;
			//Terminate _camHandle;
			_civil setPos getMarkerPos "CivilPhantomZone";
            player cameraEffect["terminate","back"];
            camDestroy _camera;
			"-5-" call debug;
			
			
            // wait until the agent died
            waitUntil{ !(alive player) or (CurrentCivil!=_civil) };
			
			sleep 0.2;
			
			// if the agent change civil
			if(CurrentCivil!=_civil)then{
				// replace the agent by the civil
				_civil setPos (getPos player);
				breakOut "civilLoop";
			};
			

            _finalPosPlayer=(getPos player);
            _finalDirPlayer=(getDir player);

 
            player setPos [0,0,0];
            // replace the civil body in the place where the agent were killed
            _civil switchMove "FXStandDip";
            _civil setDamage 1;

            _civil setdir _finalDirPlayer;
            _civil setPos _finalPosPlayer;

            waitUntil{ (alive player) }; // wait for the agent to respawn

            sleep 60;
            deleteVehicle _civil;
			
			breakOut "civilLoop";
			
		};
};

deleteVehicle _trg;

//if(_civil in CivilsDetectingHumans) then {
    
  

    

//};